package org.bite.lesson08;
import javax.swing.*;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.PriorityBlockingQueue;
import java.util.concurrent.TimeUnit;

/**
 * 自定义 定时器
 */
public class MyTimer {
    /**
     * 用阻塞队列来组织任务
     */
    private BlockingQueue<MyTask> queue = new PriorityBlockingQueue<>();
    private Object locker = new Object();
    public MyTimer(){
        Thread thread = new Thread(()->{
            while(true){
                try {
                    MyTask task = this.queue.take();
                    long currentTime = System.currentTimeMillis();
                    if(currentTime >= task.getTime()){
                        System.out.println("执行任务");
                        task.getRunnable().run();
                    }else{
                        long waitTime = task.getTime() - currentTime;
                        queue.put(task);
                        synchronized (locker){
                            locker.wait(waitTime);
                        }
                    }
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
            }
        }, "扫描任务的线程");
        // 启动线程，真正去系统中申请资源
        thread.start();

        // 创建一个后台线程
        Thread daemonThread = new Thread(()->{
            while(true){
                synchronized (locker){
                    locker.notifyAll();
                }
                try{
                    TimeUnit.MILLISECONDS.sleep(100);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        });
        //设置为后台线程
        daemonThread.setDaemon(true);
        daemonThread.start();
    }

    /**
     * 添加任务的方法
     * @param runnable 任务
     * @param delay 延时
     * @throws InterruptedException
     */
    public void schedule(Runnable runnable, long delay) throws InterruptedException{
        MyTask myTask = new MyTask(runnable, delay);
        queue.put(myTask);
        System.out.println("成功添加一个任务~");
        synchronized(locker){
            locker.notifyAll();
        }
    }
}



/**
 * 用一个类来描述任务以及任务执行的时间
 */
class MyTask implements Comparable<MyTask>{
    private Runnable runnable;
    private long time;
    public MyTask(Runnable runnable, long delay){
        // 校验任务不能为空
        if (runnable == null) {
            throw new IllegalArgumentException("任务不能为空.");
        }
        // 时间不能为负数
        if (delay < 0) {
            throw new IllegalArgumentException("执行时间不能小于0.");
        }
        this.runnable = runnable;
        this.time = System.currentTimeMillis() + delay;
    }

    public Runnable getRunnable() {
        return runnable;
    }

    public long getTime() {
        return time;
    }
    @Override
    public int compareTo(MyTask o){
        if(this.getTime() > o.getTime()){
            return 1;
        }else if(this.getTime() < o.getTime()){
            return -1;
        }else{
            return 0;
        }
    }
}
